﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QInputDialog>
#include "Dialog_AddProxy.h"
#include "QThread"
#include "QMessageBox"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}



//连接映射服务器
void MainWindow::on_pushButton_connect_clicked()
{
    ui->label_tip->setText(tr("正在连接服务器,请稍等..."));
    if(ui->lineEdit_host->text().isEmpty() || ui->lineEdit_port->text().toUInt() < 1){
        ui->label_tip->setText(tr("无效的地址与端口！"));
        return;
    }

    QString t_id =  QInputDialog::getText(this, tr("输入字符串对话框"), tr("请输入用户名："), QLineEdit::Normal,"");

    if(t_id.isEmpty()){
        ui->label_tip->setText(tr("用户名无效！"));
        return;
    }

    //创建连接对象，判断是否能够连接成功
    client = new TcpClient();
    if(!client->connectServer(QHostAddress(ui->lineEdit_host->text()),ui->lineEdit_port->text().toUInt(),t_id)){
        ui->label_tip->setText(tr("无法连接服务器，连接失败！"));
        return;
    }

    //连接成功，禁用连接按钮
    ui->lineEdit_host->setEnabled(false);
    ui->lineEdit_port->setEnabled(false);
    ui->pushButton_connect->setEnabled(false);
    ui->label_tip->setText("");

    //绑定清理回调事件
    client->Fun_ClearList = [=](){
        ui->treeWidget_proxy->clear();
    };

    //绑定添加子项项目
    client->Fun_addItem = [=](QString name, bool isTcp, uint16_t serPort, uint16_t localPort, bool persistent){
        QTreeWidgetItem* t_tItem = new QTreeWidgetItem;
        t_tItem->setText(0,name);
        t_tItem->setText(1,(isTcp?"TCP":"UDP"));
        t_tItem->setText(2,QString::number(serPort));
        t_tItem->setText(3,QString::number(localPort));
        t_tItem->setText(4,(persistent?"√":""));
        ui->treeWidget_proxy->addTopLevelItem(t_tItem);
    };

    //客户端断开连接
    client->Fun_disAppConnect = [=](){
        ui->lineEdit_host->setEnabled(true);
        ui->lineEdit_port->setEnabled(true);
        ui->pushButton_connect->setEnabled(true);
        ui->label_tip->setText("与服务器断开连接");
    };
}

//添加映射
void MainWindow::on_pushButton_addProxy_clicked()
{
    Dialog_AddProxy* form_add = new Dialog_AddProxy(this,
        [=](QString name,bool isTcp,uint16_t serverPort,uint16_t localProxy,bool persistent){
            if(client == nullptr || !client->isValid()) return;
            client->addProxy(name,isTcp,serverPort,localProxy,persistent);
//            QThread::msleep(100); //延迟100毫秒
//            this->on_pushButton_flashProxy_clicked();
    });
    form_add->open();
}

//移除映射
void MainWindow::on_pushButton_removeProxy_clicked()
{
    if(client == nullptr || !client->isValid()) return;
    QTreeWidgetItem *pCurrentItem = ui->treeWidget_proxy->currentItem();
    if(pCurrentItem != nullptr){
        uint16_t t_port = pCurrentItem->text(3).toUInt();
        if(t_port > 0) client->removeProxy(t_port);
        QThread::msleep(100); //延迟100毫秒
        this->on_pushButton_flashProxy_clicked();
    }
}

//刷新映射
void MainWindow::on_pushButton_flashProxy_clicked()
{
    if(client == nullptr || !client->isValid()) return;
    ui->treeWidget_proxy->clear();
    client->getProxyList(); //刷新映射信息
}

